package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.adapter.MyPostingOnCircleAdapte;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.AttentionPostBean;
import com.hangzhou.welbeing.welbeinginstrument.bean.CancelAttentionPostBean;
import com.hangzhou.welbeing.welbeinginstrument.bean.UserPostDateBean;
import com.hangzhou.welbeing.welbeinginstrument.bean.UserPostListDateBan;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 09 月 10 日
 * 作者  Z2043 welbeing to hangzou com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 *
 * @desc 我发的帖子
 */
public class MyPostingOnCircleActivity extends Activity {
    private static final String TAG = "MyPostingOnCircleActivity";
    @Bind(R.id.activity_my_posting_on_circle_iv_return)
    ImageView mActivityMyPostingOnCircleIvReturn;
    @Bind(R.id.activity_my_posting_on_circle_iv_head_portrait)
    ImageView mActivityMyPostingOnCircleIvHeadPortrait;
    @Bind(R.id.activity_my_posting_on_circle_tv_name)
    TextView mActivityMyPostingOnCircleTvName;
    @Bind(R.id.activity_my_posting_on_circle_tv_signature)
    TextView mActivityMyPostingOnCircleTvSignature;
    @Bind(R.id.activity_my_posting_on_circle_tv_state)
    TextView mActivityMyPostingOnCircleTvState;
    @Bind(R.id.activity_my_posting_on_circle_iv_add)
    ImageView mActivityMyPostingOnCircleIvAdd;

    @Bind(R.id.activity_my_posting_on_circle_ll_tv_focus_number)
    TextView mActivityMyPostingOnCircleLlTvFocusNumber;
    @Bind(R.id.activity_my_posting_on_circle_ll_focus)
    LinearLayout mActivityMyPostingOnCircleLlFocus;
    @Bind(R.id.activity_my_posting_on_circle_ll_tv_fans_number)
    TextView mActivityMyPostingOnCircleLlTvFansNumber;
    @Bind(R.id.activity_my_posting_on_circle_ll_fans)
    LinearLayout mActivityMyPostingOnCircleLlFans;
    @Bind(R.id.activity_my_posting_on_circle_recyclerView)
    RecyclerView mActivityMyPostingOnCircleRecyclerView;
    private UserPostDateBean.DataBean bean;

    @Bind(R.id.activity_my_posting_on_circle_tv_attention)
    TextView mActivityMyPostingOnCircleLlTvAttention;
    private String id1;
    private int att;
    private MyPostingOnCircleAdapte myPostingOnCircleAdapte;
    private String id2;
    private String num;
    private String att_01;
    private List<UserPostListDateBan.DataBean> mlist;
    private String cus_logo;
    private String cus_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_posting_on_circle);
        ButterKnife.bind(this);

        Utils.setStatusBar(this,false,false);
        Utils.setStatusTextColor(true,this);
        Intent intent = getIntent();
       // num = intent.getStringExtra("num");
        id2 = intent.getStringExtra("id");
        Log.e(TAG,"parseNetworkResponse=====id2==============="+id2);
        att_01 = intent.getStringExtra("att");
       /* cus_logo = intent.getStringExtra("cus_logo");
        cus_name = intent.getStringExtra("cus_name");*/

        mActivityMyPostingOnCircleLlTvFansNumber.setText(att_01);
        mActivityMyPostingOnCircleLlTvFocusNumber.setText(num);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mActivityMyPostingOnCircleRecyclerView.setLayoutManager(layoutManager);
        myPostingOnCircleAdapte = new MyPostingOnCircleAdapte(this);
        mActivityMyPostingOnCircleRecyclerView.setAdapter(myPostingOnCircleAdapte);
        myPostingOnCircleAdapte.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view) {
                int position = mActivityMyPostingOnCircleRecyclerView.getChildAdapterPosition(view);
                Intent intent = new Intent(MyPostingOnCircleActivity.this, SimilarProfilePageFocusedActivity.class);
                intent.putExtra("po_id",mlist.get(position).po_id);
                intent.putExtra("po_url",mlist.get(position).po_url);
                intent.putExtra("po_name",mlist.get(position).po_name);
                intent.putExtra("content",mlist.get(position).content);
                intent.putExtra("cus_logo",cus_logo);
                intent.putExtra("addtime",mlist.get(position).addtime);
                intent.putExtra("cus_name",cus_name);
                intent.putExtra("state",mlist.get(position).state+"");
                intent.putExtra("count",mlist.get(position).count+"");
                intent.putExtra("cus_id",mlist.get(position).cus_id+"");
                intent.putExtra("col",mlist.get(position).col+"");
                intent.putExtra("lCount",mlist.get(position).lCount+"");
                intent.putExtra("lstate",mlist.get(position).lstate+"");
                intent.putExtra("hits_like",mlist.get(position).hits_like+"");
                startActivity(intent);
            }

            @Override
            public void onItemFirstButtonClick(View v, int position) {

            }
        });
        loadUserPostDate(id2);

    }
    /**
     * @desc 用户发布圈
     * */
    private void loadUserPostListDate(int p, String id, final String cus_logo, final String cus_name) {
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.USER_POST_LIST)
                .addParams("cus_id", id)
                .addParams("p", p+"")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String parseNetworkResponse =  response.body().string();
                        Log.e(TAG,"parseNetworkResponse========"+parseNetworkResponse);
                        Gson gson =  new Gson();
                        final UserPostListDateBan userPostListDateBan =  gson.fromJson(parseNetworkResponse,UserPostListDateBan.class);
                        if(userPostListDateBan.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    mlist = userPostListDateBan.data;
                                    if(mlist != null){
                                        myPostingOnCircleAdapte.addItem(mlist,cus_logo,cus_name);
                                    }

                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyPostingOnCircleActivity.this,userPostListDateBan.errorMessage,Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    /**
     * @desc 加载用户数据
     * @param id*/
    private void loadUserPostDate(String id) {
        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.USER_POST)
                .addParams("cus_id",id)
                .addParams("id",user_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, final int id) throws Exception {
                        String parseNetworkResponse =  response.body().string();
                      //  Log.e(TAG,"parseNetworkResponse========"+parseNetworkResponse);
                        Gson gson =  new Gson();
                        final UserPostDateBean userPostDateBean =   gson.fromJson(parseNetworkResponse,UserPostDateBean.class);
                        if(userPostDateBean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    List<UserPostDateBean.DataBean>   bean = userPostDateBean.data;
                                    if(bean != null){
//                                        Glide.with(MyPostingOnCircleActivity.this).load(RBConstants.SERVER_URL+"/Public/Uploads/"+ bean.cus_logo).transform(new GlideCircleTransform(MyPostingOnCircleActivity.this)).into(mActivityMyPostingOnCircleIvHeadPortrait);
//                                        mActivityMyPostingOnCircleTvName.setText(bean.cus_name);
//                                        mActivityMyPostingOnCircleTvSignature.setText(bean.signature);
//                                        String state = bean.state;
//                                        if(state.equals("1")){
//                                            mActivityMyPostingOnCircleTvState.setText("怀孕中");
//                                        }else if(state.equals("2")){
//                                            mActivityMyPostingOnCircleTvState.setText("备孕中");
//                                        }
//                                        id1 = bean.id;
//                                        att = bean.att;
//                                        if(att == 0){
//                                            mActivityMyPostingOnCircleLlTvAttention.setText("关注");
//                                        }else if(att == 1){
//                                            mActivityMyPostingOnCircleLlTvAttention.setText("已关注");
//                                        }
//                                        SharedPreferences sp =getSharedPreferences("second_app_internal_data", 0);
//                                        SharedPreferences.Editor edit = sp.edit();
//                                        edit.putString("user_cus_logo",bean.cus_logo);
//                                        edit.putString("user_name",bean.cus_name);
//                                        edit.commit();
//
//                                        loadUserPostListDate(1,id2,bean.cus_logo,bean.cus_name);
                                    }


                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyPostingOnCircleActivity.this,userPostDateBean.errorMessage,Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
    /**
     * @desc 取消关注
     * */
    private void cancelAttentionPost(String id) {
        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.CANCLE_ATTENTION_POST)
                .addParams("cus_id",user_id)
                .addParams("followed", id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String parseNetworkResponse =  response.body().string();
                       // Log.e(TAG,"parseNetworkResponse========"+parseNetworkResponse);
                        Gson gson =  new Gson();
                        final CancelAttentionPostBean cancelAttentionPostBean = gson.fromJson(parseNetworkResponse,CancelAttentionPostBean.class);
                        if(cancelAttentionPostBean.errorCode == 200){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyPostingOnCircleActivity.this,"取消关注成功",Toast.LENGTH_LONG).show();
                                   // loadGuessYouLikeDate(1);
                                    mActivityMyPostingOnCircleLlTvAttention.setText("关注");
                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyPostingOnCircleActivity.this,cancelAttentionPostBean.errorMessage,Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    /**
     * @desc 添加关注
     * */
    private void addAttentionPost(String id) {
        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.ADD_ATTENTION_POST)
                .addParams("cus_id", user_id)
                .addParams("followed",id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String parseNetworkResponse =  response.body().string();
                        //Log.e(TAG,"parseNetworkResponse========"+parseNetworkResponse);
                        Gson gson =  new Gson();
                        final AttentionPostBean attentionPostBean =  gson.fromJson(parseNetworkResponse,AttentionPostBean.class);
                        if(attentionPostBean.errorCode == 200){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyPostingOnCircleActivity.this,"关注成功",Toast.LENGTH_LONG).show();
                                    mActivityMyPostingOnCircleLlTvAttention.setText("已关注");

                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyPostingOnCircleActivity.this,attentionPostBean.errorMessage,Toast.LENGTH_LONG).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    @OnClick({R.id.activity_my_posting_on_circle_iv_return, R.id.activity_my_posting_on_circle_ll_focus,
            R.id.activity_my_posting_on_circle_ll_fans,R.id.activity_my_posting_on_circle_tv_attention})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_my_posting_on_circle_iv_return:
                finish();
                break;
            case R.id.activity_my_posting_on_circle_ll_focus: //关注
                Intent intent  =  new Intent(MyPostingOnCircleActivity.this,PayAttentionActivity.class);
                intent.putExtra("id",id2);
                startActivity(intent);
                break;
            case R.id.activity_my_posting_on_circle_ll_fans:  //粉丝
                Intent intent1  =  new Intent(MyPostingOnCircleActivity.this,FansActivity.class);
                intent1.putExtra("id",id2);
                startActivity(intent1);
                break;
            case R.id.activity_my_posting_on_circle_tv_attention: //关注
                String  Attention1 =   mActivityMyPostingOnCircleLlTvAttention.getText().toString().trim();
                if(Attention1.equals("关注")){
                    addAttentionPost(id1);
                }else if(Attention1.equals("已关注")){
                    cancelAttentionPost(id1);
                }
            break;
        }
    }
}
